<template>
  <div class="container index-wrap">
    <headerBar :title="local.purchaseWarehousing" :isNoReturn="true" @changeLan="changeLan"></headerBar>
    <div class="all" :style="{paddingTop: pdt+'px',marginTop: '10px'}">
      <div class="page__bd" style="width: 100%;">
        <div class="weui-search-bar">
          <div class="weui-search-bar__form">
            <div class="weui-search-bar__box">
              <input
                type="search"
                class="weui-search-bar__input"
                v-model="inputVal"
                :placeholder="local.enterthepurchasestockin"
                @confirm="inputTyping"
              />
              <div class="weui-icon-clear" v-if="inputVal.length > 0" @click="clearInput">
                <icon type="clear" size="14"></icon>
              </div>
              <span
                style=";position:absolute;right: 11px;bottom: 2px;"
                :hidden="!inputShowed"
                @click="hideInput"
              ></span>
            </div>
            <label class="weui-search-bar__label" :hidden="inputShowed" @click="showInput">
              <icon class="weui-icon-search" type="search" size="14"></icon>
              <div class="weui-search-bar__text">{{local.search}}</div>
            </label>
          </div>
          <div
            class="weui-search-bar__cancel-btn"
            :hidden="!inputShowed"
            @click="inputTyping"
          >{{local.search}}</div>
        </div>
      </div>
      <div class="filter">
        <div class="filter-left">
          <div>{{local.screen}}：</div>
          <div @click="openDialog">{{local.time}}</div>
          <div>|</div>
          <div @click="openDialog">{{local.supplier}}</div>
        </div>
        <div class="clear-filter" @click="clearChoose">{{local.clearfilter}}</div>
      </div>
      <template v-if="stockList.length == 0">
        <div class="stock-in-record nodata-wrap">
          <img src="/static/style/block.png" class="nodata-img" />
          <p class="nodata-txt">{{local.nodata}}</p>
        </div>
      </template>
      <div class="stock-in-list" v-else>
        <div
          class="stock-in-record"
          @click="routerToUrl(`../../pages/stockInDetailsSubmit/index?id=${item.id}`)"
          v-for="(item, i) in stockList"
          :key="i"
        >
          <div class="record-Number">
            <div class="a">{{local.stockInNo}}：{{item.code}}</div>
            <div class="b">{{item.createTime}}</div>
          </div>
          <div
            class="record-supplier"
          >{{local.supplier}}：{{item.supplierName}} ( {{item.supplierCode}} )</div>
          <div class="record-goods">{{local.goodsName}}：{{item.goodsName}}</div>
          <div class="record-warehouse">{{local.buyArea}}：{{item.buyAreaName}}</div> 
          <div class="record-warehouse">{{local.warehouseIn}}：{{item.warehouseName}}</div>
        </div>
      </div>

      <div class="bottom-bar">
        <div
          class="box"
          @click="routerToUrl('../../pages/stockInDraft/index')"
        >{{local.drafts}}（{{draftsCount}}）</div>
        <div class="btn-wrap">
          <div @click="routerToUrl('../../pages/addStock/index')">{{local.newStockIn}}</div>
        </div>
      </div>
    </div>
    <!--    modal-->
    <div class="modal" v-if="modalFlag" catchtouchmove="true">
      <div style="width: 100%; height: 100%;" @click="modalFlag = false"></div>
      <div class="dialog">
        <div class="tip-name">{{local.stockInNo}}:</div>
        <input v-model="code" />
        <div class="tip-name">{{local.selectSupplier}}:</div>
        <input @click="supplierHandler()" v-model="mydata.name" disabled />
        <div class="tip-name">{{local.selectTime}}:</div>
        <picker
          class="weui-btn flex"
          mode="date"
          :value="startTime"
          start="1999-01-01"
          end="2099-01-01"
          @change="DateChange"
        >
          <input disabled v-model="startTimeFormat" />
        </picker>
        <div class="tip-name">-</div>
        <picker
          class="weui-btn flex"
          mode="date"
          :value="endTime"
          start="1999-01-01"
          end="2099-01-01"
          @change="DateChangeEnd"
        >
          <input disabled v-model="endTimeFormat" />
        </picker>
        <div class="btn">
          <div class="confirm-btn" @click="closeDialog">{{local.cancel}}</div>
          <div class="confirm-btn" @click="searchBytip()">{{local.confirm}}</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import card from '@/components/card'
import headerBar from '@/components/headerBar'
import home from '@/api/home'
import { formatDate } from '@/utils/index'
export default {
  components: {
    card,
    headerBar
  },
  onShow() {
    this.initLang()
    this.getDraftList()
    this.initMyData()
  },
  computed:{
    startTimeFormat(){
      return wx.$timeFormat(this.startTime)
      // return new Date(this.startTime).$format('dd/MM/yyyy')
    },
    endTimeFormat(){
      return wx.$timeFormat(this.endTime)
      // return new Date(this.endTime).$format('dd/MM/yyyy')
    }
  },
  methods: {
    initLang(){
      this.local = wx.getStorageSync('local')
    },
    initMyData() {
      var pages = getCurrentPages()
      var currPage = pages[pages.length - 1] //当前页面
      let myData = currPage.data.mydata
      this.mydata.id = myData ? myData.id : ''
      this.mydata.name = myData ? myData.name : ''
    },
    supplierHandler() {
      // 跳转到 供应商列表
      wx.navigateTo({
        url: `../../pages/selectSupplier/index?name=supplier`
      })
    },
    // 清空搜索条件
    clearChoose() {
      this.startTime = '' //清空开始时间
      this.endTime = '' //清空结束时间
      this.code = '' //清空入库单号
      this.mydata.id = '' //清空供应商ID
      this.mydata.name = '' // 清空供应商名称
      this.stockList = [] //清空当前页面的数据
      this.getInStockList() // 重新拉取数据
    },
    openDialog() {
      this.modalFlag = true
    },
    closeDialog() {
      this.modalFlag = false
    },
    async getDraftList () {
      home.getDraftList().then(res => {
        const { rows } = res
        this.draftsCount = rows.length
      })
    },
    async searchBytip() {
      wx.showLoading({title: this.local.loading})
      // 这里面是查询, 供应商选择需要跳转到新的客户选择
     home.getInStockList({
        code: this.code || '',
        'params[startTime]': this.startTime,
        'params[endTime]': this.endTime,
        supplierId: this.mydata.id
      }).then(res => {
        wx.hideLoading()
        const { rows, code, msg } = res
        this.modalFlag = false
        if (code !== 0) {
          wx.showToast({
            title: msg || 'error',
            icon: 'none',
            duration: 2000
          })
          return
        }
        rows.forEach(item=>{
          item.createTime = item.createTime ? wx.$timeFormat(item.createTime,'time') : '' //new Date(item.createTime).$format('dd/MM/yyyy') : ''
        })
        this.stockList = rows
      })
    },
    changeLan(val) {
      this.local = val
    },
    routerToUrl(url) {
      wx.navigateTo({
        url
      })
    },
    async getInStockList () {
      wx.showLoading({title: this.local.loading})
      home.getInStockList({
        pageSize: this.pageSize,
        pageNum: this.pageNum,
        'params[startTime]': this.startTime,
        'params[endTime]': this.endTime,
        supplierId: this.mydata.id
      }).then(res => {
        wx.hideLoading()
        const { rows, code, msg, total } = res
          rows.forEach(item=>{
          item.createTime = item.createTime ? wx.$timeFormat(item.createTime,'time') : '' //new Date(item.createTime).$format('dd/MM/yyyy hh:ss') : ''
          })
        if (code !== 0) {
          wx.showToast({
            title: msg || 'error',
            icon: 'none',
            duration: 2000
          })
          return
        }
        this.total = total
        if (this.pageNum == 1) {
          this.stockList = rows
        } else {
          this.stockList = this.stockList.concat(rows)
        }
      })
    },
    async getInStockListBySupplier(inputVal) {
      this.supplierId = ''
      this.startTime = ''
      this.endTime = ''
      this.code = inputVal
      this.searchBytip()
    },
    DateChange(e) {
      this.startTime = e.mp.detail.value
    },
    DateChangeEnd(e) {
      this.endTime = e.mp.detail.value
    },
    showInput() {
      this.inputShowed = true
    },
    hideInput() {
      this.inputVal = ''
      this.inputShowed = false
    },
    clearInput() {
      this.inputVal = ''
    },
    inputTyping(e) {
      this.getInStockListBySupplier(this.inputVal)
    }
  },
  pageReturn() {
    this.pageNum = 1
    this.pageSize = 7
  },
  data() {
    return {
      mydata: {
        id: '',
        name: ''
      },
      total: 0,
      pageNum: 1,
      pageSize: 7,
      draftsCount: 0,
      isBlock: false,
      startTime: '',
      endTime: '',
      modalFlag: false,
      pdt: 66,
      local: wx.getStorageSync('local'),
      inputShowed: false,
      inputVal: '',
      logs: [],
      time: '12:01',
      stockList: []
    }
  },
  async onReachBottom() {
    if (
      this.pageNum < parseInt((this.total + this.pageSize - 1) / this.pageSize)
    ) {
      // 如果当前页面小于最后一页
      this.pageNum = this.pageNum + 1 // 把当前页面，变成下一页面的页面数
      this.getInStockList() // 进行对后台数据的请求
    } else {
      wx.showToast({
        // 如果当前页面处于最后一页，就提示没有数据了
        title: '没有更多了',
        icon: 'none',
        duration: 2000
      })
    }
  },
  // 下拉刷新
  async onPullDownRefresh() {
    this.pageNum = 1 // 只请求第一页面的列表数据
    this.getInStockList() // 进行数据的请求
    wx.stopPullDownRefresh() // 停止下拉刷新
  },
  onLoad() {
    this.initLang()
    var myDate = new Date()
    this.startTime = formatDate(myDate, 'YYYY-MM-DD')
    this.endTime = formatDate(myDate, 'YYYY-MM-DD')
    this.pdt = this.globalData.CustomBar
    this.getInStockList()
  },
  created() {}
}
</script>

<style scoped lang="scss">
@import 'index';
.log-list {
  display: flex;
  flex-direction: column;
  padding: 40rpx;
}

.log-item {
  margin: 10rpx;
}
</style>
